import eslint from '@eslint/js'
import tseslint from 'typescript-eslint'
import vueEslint from 'eslint-plugin-vue'
import prettier from 'eslint-config-prettier'

// 安全获取Vue 3 essential规则
const getVue3EssentialConfig = () => {
  if (Array.isArray(vueEslint.configs['vue3-essential'])) {
    return vueEslint.configs['vue3-essential']
  }
  if (typeof vueEslint.configs['vue3-essential'] === 'object') {
    return [vueEslint.configs['vue3-essential']]
  }
  return [{ rules: {} }]
}

export default [
  // 忽略文件配置
  {
    ignores: [
      'node_modules/',
      'dist/',
      'build/',
      '*.d.ts',
      '*.min.js',
      'coverage/',
      '.git/',
      'public/',
    ],
  },

  // 基础ESLint规则
  eslint.configs.recommended,

  // TypeScript规则
  ...tseslint.configs.recommended,

  // Vue 3规则
  ...getVue3EssentialConfig(),

  // Prettier规则
  prettier,

  // 自定义配置
  {
    files: ['**/*.js', '**/*.jsx', '**/*.ts', '**/*.tsx', '**/*.vue'],

    languageOptions: {
      globals: {
        browser: true,
        es2021: true,
        node: true,
        jest: true,
      },

      parser: vueEslint.parser,
      parserOptions: {
        ecmaVersion: 'latest',
        sourceType: 'module',
        parser: tseslint.parser,
        jsxPragma: 'React',
        ecmaFeatures: {
          jsx: true,
        },
      },
    },

    plugins: {
      vue: vueEslint,
      '@typescript-eslint': tseslint.plugin,
    },

    rules: {
      // ESLint核心规则
      'no-var': 'error',
      'no-multiple-empty-lines': ['warn', { max: 1 }],
      'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
      'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
      'no-unexpected-multiline': 'error',
      'no-useless-escape': 'off',

      // TypeScript规则
      '@typescript-eslint/no-unused-vars': 'error',
      '@typescript-eslint/prefer-ts-expect-error': 'error',
      '@typescript-eslint/no-explicit-any': 'off',
      '@typescript-eslint/no-non-null-assertion': 'off',
      '@typescript-eslint/no-namespace': 'off',
      '@typescript-eslint/semi': 'off',

      // Vue规则（已移除废弃的script-setup-uses-vars）
      'vue/multi-word-component-names': 'off',
      'vue/no-mutating-props': 'off',
      'vue/attribute-hyphenation': 'off',
    },
  },
]
